% File: mimochan_filter.m
% -----------------------
% This script implements mimo channel filtering, using filter function for
% each individual link.

% function [fadesig h h_sq_norm] = mimochan_filter(chan_mimo, xtx)
% [nr nt] = size(chan_mimo);
% path_delays_norm = chan_mimo{1,1}.PathDelays/chan_mimo{1,1}.InputSamplePeriod;
% path_delays_norm = round(path_delays_norm);
fadesig = zeros(nr,nt,size(xtx,1)); % fading signal for each link
h = zeros(nr,nt,length_chan); % channel impulse response for each link
for p = 1: nr
    for q = 1: nt
        fadesig(p,q,:) = filter(chan_mimo{p,q},xtx(:,q));
%         h(p,q,:) = chan_mimo{p,q}.PathGains(1,:);
        tao_norm = chan_mimo{p,q}.PathDelays/Ts;
        nrange = (-1*chan_mimo{p,q}.ChannelFilterDelay:(length_chan-chan_mimo{p,q}.ChannelFilterDelay-1));
        h(p,q,:) = chan_mimo{p,q}.PathGains(1,:)*sinc(repmat(tao_norm.',[1,length(nrange)])-repmat(nrange,[length(tao_norm),1])); 
    end
end
fadesig = squeeze(sum(fadesig,2)).';
fadesig = reshape(fadesig,[],nr);
h_sq_norm = sum(abs(h).^2,3); % channel pulse response energy

% End of script